<?php
/**
 * FMS Masterplan CORE
 * @package Core
 * @author Urban Soban
 * @version 0.1
 * @copyright 2009 Urban Soban
 */

class Core {
	public static $FMSConfig;
	private $BodyApplication;
	private $AjaxRequest = false;

	function __construct(FMSConfig $Config){
		self::$FMSConfig = $Config;

		$this->init();
	}

	function init(){
		// Get files and include them
		$IncludeFiles = self::$FMSConfig->getType("boot");

		foreach ($IncludeFiles as $file){
			require_once(dirname(__FILE__)."/".$file["value"]);
		}

		// Initialize System registry and set environment varibles
		$SystemRegistry = SystemRegistry::getInstance();

		$EnvironmentVaribles = self::$FMSConfig->getType("environment");

		foreach ($EnvironmentVaribles as $ENVName => $ENValue) {
			$SystemRegistry->set($ENVName, $ENValue["value"]);
		}

		$SmartyRequest = new Request("smarty");
		RequestQueue::push($SmartyRequest);

		$this->redirections();

		// Lock System registry
		$SystemRegistry->lock();
	}

	function start(){
		RequestHandler::process();
	}

	function render(){

		$Smarty = ApplicationRegistry::getInstance()->get("Smarty");

		$Body = ResultRegistry::getInstance()->get($this->BodyApplication);

		if($this->AjaxRequest == true){
			echo $Body;
			return true;
		}

		if($Body == false){
			$BodyRequest = new Request("content", "loadArticleToTemplate", "1");
			$Body = RequestHandler::interrupt($BodyRequest, 1);
		}

		$Smarty->assign("body", $Body);

		$Body = $Smarty->fetch("index.tpl");
		$Head = $Smarty->fetch("head.tpl");

		echo $Head.$Body;
	}

	function redirections(){
		$HTTP = new HTTPRequest();

		if($HTTP->get(0) == false){
			return true;
		}

		// Look for redirections
		$RedirectionsFile = dirname(__FILE__)."/../settings/redirections.xml";
		$RedirectionsXML = @simplexml_load_file($RedirectionsFile);

		if(!is_object($RedirectionsXML)){
			throw new FMSException("Cannot load < redirections.xml >", 5);
		}

		$RedirectionHit = false;

		for($i = 0; (($HTTP->get($i) != false) && ($RedirectionHit == false)); $i++){
			$xpath = $RedirectionsXML->xpath("//rule[@navnode=\"$i\"]");

			if(count($xpath) > 0){
				$RequestPart = $HTTP->get($i);
				foreach ($xpath as $Rule){
					if((string)$Rule == $RequestPart){
						$RedirectionSettings = $Rule->attributes();

						$Application = (string)$RedirectionSettings["runapp"];
						$Method = (isset($RedirectionSettings["method"])) ? (string)$RedirectionSettings["method"] : null;
						$Parameters = (isset($RedirectionSettings["parameters"])) ? explode(";",$RedirectionSettings["parameters"]) : null;

						$Request = new Request($Application, $Method, $Parameters);
						RequestQueue::push($Request);
						$RedirectionHit = true;

						SystemRegistry::getInstance()->set("url_position", $i+1);
						$this->BodyApplication = $Application;
					}
				}
			}
		}

		// If redirection is not found, load called application
		if($RedirectionHit == false){
			$URLPosition = 0;
			$URLPart = $HTTP->get($URLPosition);

			if($URLPart == "ajax"){
				$URLPosition++;
				$this->AjaxRequest = true;
				$URLPart = $HTTP->get($URLPosition);
			}

			$Request = new Request($URLPart);
			RequestQueue::push($Request);
			SystemRegistry::getInstance()->set("url_position", $URLPosition+1);
			$this->BodyApplication = $URLPart;
		}
	}
}

?>